package cn.tedu.egweibo3.controller;

import cn.tedu.egweibo3.common.response.JsonResult;
import cn.tedu.egweibo3.common.response.StatusCode;
import cn.tedu.egweibo3.mapper.CommentMapper;
import cn.tedu.egweibo3.pojo.dto.CommentDTO;
import cn.tedu.egweibo3.pojo.entity.Comment;
import cn.tedu.egweibo3.pojo.vo.CommentVO;
import cn.tedu.egweibo3.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "评论模块")
@Slf4j
@RestController
@RequestMapping("/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @ApiOperation("发布评论")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody CommentDTO commentDTO,@ApiIgnore HttpSession session){
        log.debug("commentDTO="+commentDTO);
        UserVO userVO = (UserVO)session.getAttribute("user");
        if (userVO!=null){
            Comment comment = new Comment();
            BeanUtils.copyProperties(commentDTO,comment);
            comment.setCreated(new Date());
            comment.setUserId(userVO.getId());
            commentMapper.insert(comment);
            return new JsonResult(StatusCode.OPERATION_SUCCESS);
        }
        return new JsonResult(StatusCode.NOT_LOGIN);
    }

    @ApiOperation("获取评论列表")
    @GetMapping("selectByWeiboId")
    @ApiImplicitParam(name = "id",value = "微博id",required = true,dataType = "Long")
    public JsonResult selectByWeiboId(Long id){
        List<CommentVO> commentVOList = commentMapper.selectByWeiboId(id);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,commentVOList);
    }
}
